/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode() {}
 *     ListNode(int val) { this.val = val; }
 *     ListNode(int val, ListNode next) { this.val = val; this.next = next; }
 * }
 */
class Solution {
    public ListNode reverseKGroup(ListNode head, int k) {
        if(head == null || head.next == null) return head;

        ListNode temp = head;
        for ( int i = 0; i < k;i++){
            if(temp == null) return head;
            temp=temp.next;
        }

        //创建newhead 来存放
        ListNode newHead = rever(head,temp);
        //头指向k+1的位置
        head.next = reverseKGroup(temp,k);
        //
        return newHead;

    }
    public ListNode rever(ListNode head ,ListNode temp){
        //定义两个指针，来保证循环的完成
        ListNode pre = null ,next = null;
        //不断循环，直到 head到temp的位置，
        while (head != temp){
            //内执行，翻转
            next = head.next;
            head.next = pre;
            pre = head ;
            head = next;
        }
        return pre;
    }
}